package com.sduept.nwld.dataserver.manager.statistics;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

import com.sduept.cim.ledger.total.model.PrimaryDeviceCount;
import com.sduept.cim.utils.CimUtils;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.cim.PrimaryDeviceCountPersistenceManager;
import com.sduept.nwld.dataserver.manager.cim.PrimaryDeviceCountQueryManager;
import com.sduept.nwld.dataserver.model.countanalyse.BusbarInfo;
import com.sduept.nwld.dataserver.model.countanalyse.TansformerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TransmissionEquipmentManager {

	@Autowired
	private SuperDAO dao;

	@Inject
	private PrimaryDeviceCountQueryManager pdquery;

	public List<TansformerInfo> getAllTansformerInfo() {
		return dao.findAll(TansformerInfo.class);
	}

	public TansformerInfo createOrUpdateTansformerInfo(TansformerInfo tl) {
		return dao.createOrUpdate(tl, TansformerInfo.class);
	}

	public void deleteTansformerInfo(String id) {
		dao.delete(id, TansformerInfo.class);
	}

	public void deleteTansformerInfoByYear(Integer year) {
		dao.delete(TansformerInfo.class, "year=?1", new Object[] {year});
	}

	/**
	 *  根据一次设备统计结果重新计算
	 * @param year
	 */
	public void countByPrimaryDeviceCount(int year) {
		countTansformerInfoByPrimaryDeviceCount(year);
		countBusbarInfoByPrimaryDeviceCount(year);

	}
	public void countTansformerInfoByPrimaryDeviceCount(int year) {
		deleteTansformerInfoByYear(year);
		List<PrimaryDeviceCount> list =  pdquery.countByEquipmentType(year,PrimaryDeviceCountPersistenceManager.StatisticType.powertransformer.getType());
		List<TansformerInfo> lastYearList = getTansformerInfos(year-1,null);
		list.stream().forEach(action->{
			TansformerInfo ei = new TansformerInfo();
			ei.setVoltage((int) CimUtils.getStandVoltagelevel(action.getVoltageLevel()));
			ei.setYear(year);
			ei.setCount(action.getStatisticCount().intValue());
			yearIncreaseCount(lastYearList,ei);
			createOrUpdateTansformerInfo(ei);
		});
	}
	public void countBusbarInfoByPrimaryDeviceCount(int year) {
		deleteBusbarInfoByYear(year);
		List<PrimaryDeviceCount> list =  pdquery.countByEquipmentType(year,PrimaryDeviceCountPersistenceManager.StatisticType.busbarsection.getType());
		List<BusbarInfo> lastYearList = getBusbarInfos(year-1,null);
		list.stream().forEach(action->{
			BusbarInfo ei = new BusbarInfo();
			ei.setVoltage((int) CimUtils.getStandVoltagelevel(action.getVoltageLevel()));
			ei.setYear(year);
			ei.setCount(action.getStatisticCount().intValue());
			yearIncreaseCount(lastYearList,ei);
			createOrUpdateBusbarInfo(ei);
		});
	}
	private void yearIncreaseCount(List<TansformerInfo> lastYearList,TansformerInfo ei) {
		ei.setNewAdd(ei.getCount());
		lastYearList.forEach(lt->{
			if((ei.getYear()-1) == lt.getYear()) {
				ei.setNewAdd(ei.getCount()-lt.getCount());
				return ;
			}
		});
	}
	private void yearIncreaseCount(List<BusbarInfo> lastYearList,BusbarInfo ei) {
		ei.setNewAdd(ei.getCount());
		lastYearList.forEach(lt->{
			if((ei.getYear()-1) == lt.getYear()) {
				ei.setNewAdd(ei.getCount()-lt.getCount());
				return ;
			}
		});
	}

	public List<TansformerInfo> getTansformerInfos(Integer year, Integer voltage) {
		String esql = " 1=1 ";
		List<Object> param = new ArrayList<Object>();
		if(null != year) {
			esql += " and year = ?";
			param.add(year);
		}
		if(null != voltage) {
			esql += " and voltage = ?";
			param.add(voltage);
		}
		return dao.findByCondition(TansformerInfo.class, esql, param.toArray());
	}

	public List<BusbarInfo> getAllBusbarInfo() {
		return dao.findAll(BusbarInfo.class);
	}

	public BusbarInfo createOrUpdateBusbarInfo(BusbarInfo tl) {
		return dao.createOrUpdate(tl, BusbarInfo.class);
	}

	public void deleteBusbarInfo(String id) {
		dao.delete(id, BusbarInfo.class);
	}

	public void deleteBusbarInfoByYear(Integer year) {
		dao.delete(BusbarInfo.class, "year=?1", new Object[] {year});
	}

	public List<BusbarInfo> getBusbarInfos(Integer year, Integer voltage) {
		String esql = " 1=1 ";
		List<Object> param = new ArrayList<Object>();
		if(null != year) {
			esql += " and year = ?";
			param.add(year);
		}
		if(null == voltage) {
			voltage = 0;
		}
		if(null != voltage) {
			esql += " and voltage = ?";
			param.add(voltage);
		}
		return dao.findByCondition(BusbarInfo.class, esql, param.toArray());
	}

}
